package org.yx.service.smat.save_product_manufacture_material;

import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.dbist.dml.Query;
import org.yx.exception.BizException;
import org.yx.models.SmatBomDat;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保存BOM
 *
 * @author zxw
 * @since 2023/03/21
 **/
public class SmatSaveProductManufactureMaterialService extends BaseService {
    public SmatSaveProductManufactureMaterialOut saveProductManufactureMaterial(SmatSaveProductManufactureMaterialIn in) throws Exception {
        if (CollUtil.isEmpty(in.getBomList())) {
            throw new BizException("SYS-0004", FieldMap.of("parameter", "bomList"));
        }

        Map<String, List<SmatBomDat>> oldBomMap = new HashMap<String, List<SmatBomDat>>();
        List<SmatBomDat> upBomList = new ArrayList<SmatBomDat>();
        List<SmatBomDat> insBomList = new ArrayList<SmatBomDat>();
        for (SmatBomDat inBom : in.getBomList()) {
            String prod = inBom.getProdCode() + ":" + inBom.getProdVer();
            if (!oldBomMap.containsKey(prod)) {
                Query query = new Query();
                query.addFilter("orgCode", in.getUserOrgCode());
                query.addFilter("prodCode", inBom.getProdCode());
                query.addFilter("prodVer", inBom.getProdVer());

                List<SmatBomDat> oldBomList = selectList(SmatBomDat.class, query);
                oldBomMap.put(prod, oldBomList);
                if (CollUtil.isNotEmpty(oldBomList)) {
                    //先删除旧的数据
                    for (SmatBomDat bom : oldBomList) {
                        bom.setStatus('D');
                    }

                    updateBatch(oldBomList, "status");
                }
            }

            inBom.setOrgCode(in.getUserOrgCode());
            inBom.setStatus('N');
            inBom.setAltFlag('N');
            if (inBom.getLossQty() == null) {
                inBom.setLossQty(BigDecimal.ZERO);
            }
            if (inBom.getLossRate() == null) {
                inBom.setLossRate(BigDecimal.ZERO);
            }

            boolean isExist = false;
            if (CollUtil.isNotEmpty(oldBomMap.get(prod))) {
                for (SmatBomDat oldBom : oldBomMap.get(prod)) {
                    if (StrUtil.equals(oldBom.getMatCode(), inBom.getMatCode()) && StrUtil.equals(oldBom.getMatVer(), inBom.getMatVer())) {
                        isExist = true;
                        inBom.setAltFlag(oldBom.getAltFlag());
                        upBomList.add(inBom);
                        break;
                    }
                }
            }

            if (isExist) {
                continue;
            }
            insBomList.add(inBom);
        }

        if (CollUtil.isNotEmpty(upBomList)) {
            updateBatch(upBomList);
        }
        if (CollUtil.isNotEmpty(insBomList)) {
            insertBatch(insBomList);
        }

        SmatSaveProductManufactureMaterialOut out = new SmatSaveProductManufactureMaterialOut();

        return out;
    }
}